package blackjack;

import java.util.ArrayList;

public class Hand {
	ArrayList<Card> cards = new ArrayList<Card>();
	int value;
	int aceCount;

	public Hand(Card card1, Card card2) {
		cards.add(card1);
		cards.add(card2);
		value = card1.playValue + card2.playValue;
		if (card1.playValue == 11)
			aceCount++;
		if (card2.playValue == 11)
			aceCount++;
	}

	void addCard(Card card) {
		cards.add(card);
		value += card.playValue;
		if (card.playValue == 11)
			aceCount++;
	}

	public boolean hasAce() {
		for (int i = 0; i < cards.size(); i++) {
			if (cards.get(i).playValue == 11) {
				return true;
			}
		}
		return false;
	}

	int getValue() {
		if (isBusted() && aceCount > 0) {
			value -= 10;
			aceCount--;
			return value;
		}
		return this.value;
	}

	public boolean hasBlackjack() {
		return (cards.size() == 2 && value == 21);
	}

	public String toString() {
		String ret = ("");
		for (int i = 0; i < cards.size(); i++) {
			ret += (cards.get(i));
			if (i < cards.size() - 1)
				ret += (", ");
			
			
		}
		ret += " Hand Value is: " + getValue();
		if (isBusted())
			ret+= " BUSTED!";
		if (hasBlackjack())
			ret+= " BLACKJACK!";
		return ret;
	}

	public boolean isBusted() {
		return (value > 21);
	}
}
